<?php
$mysql = new mysqli("localhost", "root", "", "yihe");
if ($mysql -> connect_error) {
	exit("数据库连接失败，失败原因是{$mysql->connect_error}");
}
$mysql -> set_charset("utf8");
$flag = $_GET["flag"];
//$flag="select";
switch ($flag) {
	case 'insert' : {
		insert();
		break;
	}
	case "delete" : {
		delete();
		break;
	}
	case "update" : {
		update();
		break;
	}
	case "select" : {
		select();
		break;
	}
	default :
		break;
}
//	向数据库增加数据
function insert() {
	global $mysql;
	$trade_img = $_GET["trade_img"];
	$trade_name = $_GET["trade_name"];
	$trade_size = $_GET["trade_size"];
	$trade_color = $_GET["trade_color"];
	$trade_num = $_GET["trade_num"];
	$trade_price = $_GET["trade_price"];
	$trade_yige=$_GET["trade_yige"];
	$trade_zongjia=$_GET["trade_zongjia"];
	$delete_sql = "delete from shoppings";
	$result = $mysql -> query($delete_sql);
	if ($result) {
		$insert_sql = "insert into shoppings (trade_img,trade_name,trade_size,trade_color,trade_num,trade_price,trade_zongjia,trade_yige) values ('{$trade_img}','{$trade_name}',{$trade_size},'{$trade_color}',{$trade_num},{$trade_price},{$trade_zongjia},{$trade_yige})";
		$result = $mysql -> query($insert_sql);
		if ($result) {
			echo "1";
		} else {
			echo "2";
		}
	} else {
		echo "3";
	}
	$mysql -> close();
}

//  向数据库删除数据
function delete() {
	global $mysql;
	$trade_img = $_GET["trade_img"];
	$trade_name = $_GET["trade_name"];
	$trade_size = $_GET["trade_size"];
	$trade_color = $_GET["trade_color"];
	$trade_num = $_GET["trade_num"];
	$trade_price = $_GET["trade_price"];
	$trade_zongjia=$_GET["trade_zongjia"];
	$delete_sql = "delete from shoppings where trade_img='{$trade_img}' and trade_name = '{$trade_name}' and trade_size= {$trade_size} and trade_color='{$trade_color}' and trade_price = {$trade_price}";
	$result = $mysql -> query($delete_sql);
	if ($result) {
		echo "1";
	} else {
		echo "0";
	}
	$mysql -> close();
}

// 向数据库修改数据
function update() {
	global $mysql;
	$trade_img = $_GET["trade_img"];
	$trade_name = $_GET["trade_name"];
	$trade_size = $_GET["trade_size"];
	$trade_color = $_GET["trade_color"];
	$trade_num = $_GET["trade_num"];
	$trade_price = $_GET["trade_price"];
	$trade_zongjia=$_GET["trade_zongjia"];
		$select_sql = "select * from shoppings where trade_img='{$trade_img}' and trade_name = '{$trade_name}' and trade_size= {$trade_size} and trade_color='{$trade_color}' and trade_price = {$trade_price}";
	$result = $mysql -> query($select_sql);
	if ($result -> num_rows) {
		$buy_num = (int)$trade_num;
		$update_sql = "update shoppings set trade_num= {$buy_num} where trade_img='{$trade_img}' and trade_name = '{$trade_name}' and trade_size= {$trade_size} and trade_color='{$trade_color}' and trade_price = {$trade_price}";
		$result = $mysql -> query($update_sql);
		if ($result) {
			echo "1";
		} else {
			echo "更新失败";
		}
	}
	$mysql -> close();
}

//项数据库查找数据
function select() {
	global $mysql;
	$select_sql = "select * from shoppings";
	$result = $mysql -> query($select_sql);
	if ($result) {
		$expre_arr = array();
		while ($row = $result -> fetch_object()) {
			array_push($expre_arr, $row);
		}
		$json_expre = json_encode($expre_arr);
		echo $json_expre;
	}
	$mysql -> close();
}
?>